package com.yihaodian.cluster;

import java.util.List;

import com.yihaodian.common.util.NetUtil;
import com.yihaodian.index.meta.shard.Shard;

public class Node implements Comparable {

	public enum Role {
		MASTER, SLAVE
	}

	public enum MgmtStatus {
		ENABLED, DISABLED
	}

	public enum Status {
		INIT, RUNNING, STARTING_SHARD, STOPPED
	}

	public static final String DEFAULT_IP = "127.0.0.1";
	public static final int DEFAULT_PORT = 8080;

	private String id;
	private String clusterId = "DEFAULT_CLUSTER";

//	private List<Shard> shards;

	private Role role = Role.SLAVE;

	private double capacity = 1.0;

	private Status status = Status.INIT;

	private MgmtStatus mgmtStatus = MgmtStatus.ENABLED;

	private String ip = DEFAULT_IP;
	private int port = DEFAULT_PORT;

	private boolean enableMaster = true;

	public Node(int port) {
		this.ip = NetUtil.localAddress();
		this.port = port;
		this.id = getId(ip, port);
		;
	}

	public Node(String ip, int port) {
		this.ip = ip;
		this.port = port;
		this.id = getId(ip, port);
		;
	}

	public Node(String nodeId) {
		this.ip = getIp(nodeId);
		this.port = getPort(nodeId);
		this.id = getId(ip, port);
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public double getCapacity() {
		return capacity;
	}

	public void setCapacity(double capacity) {
		this.capacity = capacity;
	}

	public Status getStatus() {
		return status;
	}

	public void setStatus(Status status) {
		this.status = status;
	}

	public MgmtStatus getMgmtStatus() {
		return mgmtStatus;
	}

	public void setMgmtStatus(MgmtStatus mgmtStatus) {
		this.mgmtStatus = mgmtStatus;
	}

	public String getClusterId() {
		return clusterId;
	}

	public void setClusterId(String clusterId) {
		this.clusterId = clusterId;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public boolean isEnableMaster() {
		return enableMaster;
	}

	public void setEnableMaster(boolean enableMaster) {
		this.enableMaster = enableMaster;
	}

//	public List<Shard> getShards() {
//		return shards;
//	}
//
//	public void setShards(List<Shard> shards) {
//		this.shards = shards;
//	}

	public int compareTo(Object o) {
		if (o == null || !(o instanceof Node)) {
			return 1;
		}
		Node other = (Node) o;
		return this.id.compareTo(other.id);
	}

	public static String getId(String ip, int port) {
		return "" + ip + ":" + port;
	}

	public static String getIp(String id) {
		if (!id.contains(":")) {
			return DEFAULT_IP;
		}
		String[] splits = id.split(":");
		if (splits == null || splits.length != 2) {
			return DEFAULT_IP;
		}
		return splits[0];
	}

	public static int getPort(String id) {
		if (!id.contains(":")) {
			return DEFAULT_PORT;
		}
		String[] splits = id.split(":");
		if (splits == null || splits.length != 2) {
			return DEFAULT_PORT;
		}
		return Integer.parseInt(splits[1]);
	}

	@Override
	public int hashCode() {
		return getId().hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;

		return getId().equals(((Node) obj).getId());
	}

	@Override
	public String toString() {
		return "Node [id=" + id + ", clusterId=" + clusterId + ", role=" + role
				+ ", capacity=" + capacity + ", status=" + status
				+ ", mgmtStatus=" + mgmtStatus + "]";
	}

}
